import { Component,OnInit} from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import 'rxjs/add/operator/switchMap';
import { GameService } from '../../service/game_details.service';
import { isBindPNService } from '../../service/isBindPN.service';
import { userCenterService } from '../../service/userCenter.service';
import { Pagination } from  '../../lib/pagination';
/* wiper插件 */
import 'swiper';
declare let $:any;
declare let Swiper:any;
@Component({
    selector: 'game_details',
    templateUrl: '../../../views/game_details/gameDetails.html',
    styleUrls: ['../../../css/game_details/game_details.css'],
    providers: [
        GameService,
        isBindPNService,
        userCenterService,
        Pagination
    ]
})
export class GameDetailsComponent implements OnInit{
    public isPayGame:any = true;
    public gid : any;
    private param : any = {};
    public name:any;
    public catagory_name:any;
    public thumb:any;
    public resource:any;
    public introduction:any;
    public sum:any;
    public count:any;
    public resource_url:any;
    public money:any;
    public price : any;

    constructor(
        private GameService : GameService,
        private userCenterService : userCenterService,
        private router : Router,
        private route: ActivatedRoute,
        private isB : isBindPNService

    ) {
        let self : any = this;
        console.log( self.route.params);
        self.gid = self.route.params.value.id;
        self.getGameDetails();
        self.getUserMsg();
        self.swipers();
        $.cookie("home","7");
    }
    ngOnInit() {
        this.swipers();
    }
    // 用户信息
    private  getUserMsg(){
        let self : any = this;
        self.id = $.cookie("id");
        console.log(self.id);
        let data : any = {
            member_id: self.id,
        };
        self.userCenterService.userMsg({
            data: data,
            success: function (result : any) {
                self.money = result.money;
            }
        });
    }
    // 取消购买
    public noBuyGame(){
        this.isPayGame = true;
    }
    // 购买游戏
    public buyGame(gameId:number){
        this.isPayGame = false;
        this.isB.isId();
        this.isB.isBindPN();
        this.gid = gameId;
    }

    public payGame(){
        let self: any = this;
        let mid: any = $.cookie("id");
        let data: any = {
            member_id : mid,
            game_id : self.gid
        };
        self.GameService.payGame({
            data: data,
            success: function (result: any) {
                if (self.money <= 0 ){
                    self.router.navigate(['/user_recharge']);
                }else {
                    console.log(result);
                    alert("购买成功");
                    self.noBuyGame();
                    self.getUserMsg();
                }
            }
        })
    }
    /**
     * 游戏详情
     *
     * @param number: gid 游戏ID
     */

    public getGameDetails() {
        let self : any = this;
        let data : any = {
             id: self.gid
        };
        self.GameService.getGameDetails({
            data: data,
            success: function (result : any) {
                 console.log(result);
                 if(result.error === 0){
                     self.name = result.data.name;
                     if (result.data.count == null){
                         self.count = 0;
                     }else {
                         self.count = result.data.count;
                     }
                     if (result.data.catagory_name == null){
                         self.catagory_name = '无类型'
                     }else{
                         self.catagory_name = result.data.catagory_name;
                     }
                     if(result.data.sum == null){
                         self.sum = 0;
                     }else {
                         self.sum = result.data.sum;
                     }
                     if (result.data.description == null){
                         self.introduction = '该游戏尚无描述';
                     }else {
                         self.introduction = result.data.description;
                     }
                     self.thumb = result.data.thumb;
                     self.resource = result.data.resource;
                     self.resource_url = result.resource_url;
                     self.price = result.data.price;
                 }
            }
        });
    }

    public swipers(){
        var swiper = new Swiper('.swiper-containers', {
            pagination: '.swiper-pagination',
            effect: 'coverflow',
            grabCursor: true,
            centeredSlides: true,
            slidesPerView: 'auto',
            paginationClickable: true,
            autoplay: 3000,
            coverflow: {
                rotate: 50,
                stretch: 0,
                depth: 100,
                modifier: 1,
                slideShadows : true
            }
        });
    }
}